<template>
    <el-row class="user-info-form" type="flex" justify="center">
        <el-col>
            <el-form label-position="top" :model="editForm" :rules="editFormRules" ref="editForm" @submit.native.prevent>
                <input type="hidden"  v-model="editForm.id">
                <el-form-item label="版本名称：" prop="name">
                    <el-input v-model="editForm.name" clearable></el-input>
                </el-form-item>
                <el-form-item label="版本号：" prop="version">
                    <el-input v-model="editForm.version" clearable></el-input>
                </el-form-item>
                <el-form-item label="文件地址：">
                    <el-input v-model="editForm.url" clearable></el-input>
                </el-form-item>
                <el-form-item label="描述：" >
                    <el-input v-model="editForm.description" clearable></el-input>
                </el-form-item>
                <el-form-item label="备注：">
                    <el-input v-model="editForm.remarks" clearable></el-input>
                </el-form-item>
                <el-form-item label-width="0" class="verifyidentity-button">
                    <el-button round @click="closeDialog()">取消</el-button>
                    <el-button type="primary" round @click="saveEdit('editForm')">确定</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
</template>
<script>

import { updateServer} from '@/api/dcx'

export default {
    mame: 'PlatTransDeviceForm',
    props: {
        editInfo: {
            type: Object,
            required: false
        },
        closeDialog: {
            type: Function,
            required: true
        },
    },
    data() {
        return {
            editForm: {
                id: '',
                name:'',
                version:'',
                url: '',
                description:'',
                remarks:'',
            },
            editFormRules: {
                name: [
                    { required: true, message: '请输入版本名称', trigger: 'blur' },
                ],
                version: [
                    { required: true, message: '请输入版本号', trigger: 'blur' },
                ],
            },
        }
    },
    created() {
        let infoObj = this.editInfo
        if(infoObj.id) {//编辑
          this.editForm.id = infoObj.id
          this.editForm.name = infoObj.name
          this.editForm.version = infoObj.version
          this.editForm.url = infoObj.url
          this.editForm.description = infoObj.description
          this.editForm.remarks = infoObj.remarks
        }
    },
    methods: {
       saveEdit(formName) {
        // 阻止事件冒泡
        event.stopPropagation()
        let that = this
        that.$refs[formName].validate((valid) => {
            if (valid) {
                let parames = that.editForm
                updateServer(parames).then(res => {
                    if( res.status === 200) {
                      that.$message({
                        type: 'success',
                        message: '操作成功!'
                      })
                      that.closeDialog(true)
                    }else{
                      that.$message.error(res.info)
                    }
                });
            } else {
                return false
            }
        });
    },
    }
}
</script>
